/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fiu.accesslog.statistics;

import edu.fiu.accesslog.parser.searchengine.SearchEngine;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

/**
 *
 * @author Hasan Mahmud @date Jun 20, 2012
 */
public class SearchEngineStatistics
{

        private HashMap<String, Info> searchEngineStatistics;
        private HashMap<String, Info> searchKeyWords;
        private static SearchEngineStatistics singletonInstance = null;

        private SearchEngineStatistics()
        {
                searchEngineStatistics = new HashMap<String, Info>(30);
                searchKeyWords = new HashMap<String, Info>(30);

        }

        public static synchronized SearchEngineStatistics getInstance()
        {
                if (singletonInstance == null)
                {
                        singletonInstance = new SearchEngineStatistics();
                }

                return singletonInstance;
        }

        public void addAccessLogEntry(String referrer, boolean isUnique)
        {
                if (SearchEngine.isASearchEngine(referrer))
                {
                        SearchEngine sEngine = SearchEngine.parse(referrer);
                        RecordUtility.addInHashMap(searchEngineStatistics, sEngine.getSearchEngineType().getName(), isUnique);


                        if (sEngine.getSearchKeyWords() != null)
                        {
                                String searchParam="";
                                for (String searchKeyWord : sEngine.getSearchKeyWords())
                                {
                                        searchParam = searchParam + " "+searchKeyWord;

                                }
                                if(searchParam.length() > 0)
                                {
                                        try
                                        {
                                                searchParam = URLDecoder.decode(searchParam, "UTF-8");
                                                RecordUtility.addInHashMap(searchKeyWords, searchParam, isUnique);
                                        }
                                        catch (Exception ex)
                                        {
                                                ex.printStackTrace();
                                        }
                                }
                        }

                }

        }

        public ArrayList<Info> getSearchEngineStatistics()
        {
                return RecordUtility.getClonedArrayList(searchEngineStatistics);
        }
        
         public ArrayList<Info> getSearchKeyWordStatistics()
        {
                ArrayList<Info> sKeyWordListClone = RecordUtility.getClonedArrayList(searchKeyWords);
                Collections.sort(sKeyWordListClone, new InfoComparatorByTotalHit(false));
                RecordUtility.trimArrayList(sKeyWordListClone, "Other", 50);
                double numOfAccess =0;
                
                for (Info temp:sKeyWordListClone)
                {
                        numOfAccess += temp.totalHit;
                }
                
                Info totalHitInfo = new Info("Total Hit", numOfAccess);
                
                sKeyWordListClone.add(0, totalHitInfo);
                //RecordUtility.printArray(sKeyWordListClone, "Key Word Search", sKeyWordListClone.size());
                
                
                return sKeyWordListClone;
        }
}
